<template>
	<view>
		<block v-if="dataBuffer">
			<view class="item" v-for="(res, index) in siteList" :key="res.id">
				<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id" @click="removeEvent(res.id, index)"></u-icon>
				<view class="top" @click="goOrider(res)">
					<view class="name">{{ res.username }}</view>
					<view class="phone">{{ res.userphone }}</view>
					<view class="tag">
						<text class="red" v-if="res.status == 1">默认</text>
						<text class="blue">{{res.label}}</text>
					</view>
				</view>
				<view class="bottom">
					{{res.location+res.useraddress}}
					<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id" @click="editEvent(res)"></u-icon>
				</view>
			</view>
		</block>
		
		<view v-else>
			暂无收货地址信息!
		</view>
		
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				siteList: [{
						status: 1,
						label: '公司',
						username: '亚瑟',
						userphone: 13716078900,
						location: '北京市市辖区朝阳区',
						useraddress: '五方桥基地'
					},
					{
						status: 0,
						label: '家',
						username: '鲁班',
						userphone: 13716078900,
						location: '北京市市辖区朝阳区',
						useraddress: '五方桥基地'
					},
					{
						status: 0,
						label: '学校',
						username: '李白',
						userphone: 13716078900,
						location: '北京市市辖区朝阳区',
						useraddress: '五方桥基地'
					},
				], //初始化地址数据
				dataBuffer: true
			};
		},
		onShow() {
			this.request_addresslist();
		},
		methods: {
			// 跳转order
			goOrider(item){
				console.log(item);
				uni.navigateTo({
					url: `/pages/order/order?data=${JSON.stringify(item)}`
				});
			},
			// 删除地址信息
			async removeEvent(value, index){
				console.log(value, index);
				const {uid, token} = uni.getStorageSync('userinfo');
				let [err, {data}] = await uni.request({
					method: 'POST',
					url: `${this.$baseApi}addressremove`,
					header: {authorization: token},
					data: {id: value}
				});
				console.log(data);
				if(data.code != 200) return;
				this.siteList.splice(index, 1);
				if(!this.siteList.length) this.dataBuffer = false;
				uni.showToast({title: data.msg});
			},
			
			// 跳转到新增地址页面
			toAddSite() {
				uni.navigateTo({
					url: './addSite?type=add'
				})
			},
			
			// 修改地址
			editEvent(value){
				console.log(value);
				uni.navigateTo({
					url: `./addSite?type=edit&data=${JSON.stringify(value)}`,
				})
			},

			// 获取地址列表
			async request_addresslist() {
				const {uid, token} = uni.getStorageSync('userinfo');
				let [err, {data}] = await uni.request({
					url: `${this.$baseApi}addresslist`,
					header: {authorization: token},
					data: {uid}
				});
				console.log(data);
				if (!data.list) {
					this.dataBuffer = false;
					return;
				}
				this.dataBuffer = true;
				
				// 解决多个默认
				let defaultArr = data.list.reduce((count, item) => {
					if(item.status) count.push(item);
					return count;
				}, []);
				if(defaultArr.length >= 2){
					defaultArr.forEach(item => item.status = false);
					defaultArr[0].status = true;
					console.table(defaultArr);
					console.table(data.list);
				}
				
				this.siteList = data.list;
			},
		}
	};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import '@/static/css/address.scss';
</style>
